package com.java.java8.func.parallelstream;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.IntStream;

public class test2 {
	
	private static List<Integer> list1 = new ArrayList<>();
	private static List<Integer> list2 = new ArrayList<>();
	private static List<Integer> list3 = new ArrayList<>();
	private static Lock lock = new ReentrantLock();

	public static void main(String[] args) {
	    IntStream.range(0, 10000).forEach(list1::add);

	    IntStream.range(0, 10000).parallel().forEach(list2::add);

	    IntStream.range(0, 10000).parallel().forEach(i -> {
	    lock.lock();
	    try {
	        list3.add(i);
	    }finally {
	        lock.unlock();
	    }
	    });

	    System.out.println("串行执行的大小：" + list1.size());
	    System.out.println("并行执行的大小：" + list2.size());
	    System.out.println("加锁并行执行的大小：" + list3.size());
	}

}
